package com.haier.npt.sso.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.haier.npt.common.JsonResult;
import com.haier.npt.sso.authentication.cache.TokenCacheService;
import com.haier.npt.sso.domain.HaierUser;
import com.haier.npt.sso.domain.SysUserWapper;
import com.haier.npt.sso.service.SysUserService;


@RestController
public class UserController {
	
	@Autowired
	private TokenCacheService tokenCacheService ; 
	
	@Autowired
	private SysUserService sysUserService ; 
		
	/**
	 * 获取用户信息
	 * @return
	 */
	@GetMapping("/user/userinfo")
	public JsonResult<?> getUserInfo(@RequestParam (required =true)String access_token) {
		SysUserWapper sysUserWapper =tokenCacheService.getLoginUserFromCache(access_token);
		JSONObject userObject = (JSONObject)sysUserWapper.getUser();
		if(userObject.containsKey("app_secret")){
			userObject.put("app_secret", "*****");
		}
		return JsonResult.success(userObject);
	}
	
	
	/**
	 * 根据userid 批量查询用户信息
	 * @param userIds
	 * @return
	 */
	@RequestMapping(value = "/internal/user/list" ,method = RequestMethod.POST)
	public JsonResult<List<HaierUser>> findUsersByIds(@RequestParam (value="userIds[]" ,required=false)List<Long> userIds) {
		List<HaierUser> userList = this.sysUserService.findUsersByIds(userIds); 
		return JsonResult.success(userList);  
	}

	/**
	 * 查询新增用户数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "/internal/user/newaddusers/count", method = RequestMethod.GET)
	public JsonResult<Integer> getNewAddUsers(@RequestParam("startDate") Long startDate, @RequestParam("endDate") Long endDate) {
		Integer newAddUsers = sysUserService.getNewAddUsers(startDate, endDate);
		return JsonResult.success(newAddUsers);
	}

	/**
	 * 查询最近登录用户数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "/internal/user/recentloginusers/count", method = RequestMethod.GET)
	public JsonResult<Integer> getRecentLoginUsers(@RequestParam("startDate") Long startDate, @RequestParam("endDate") Long endDate) {
		Integer recentLoginUsers = sysUserService.getRecentLoginUsers(startDate, endDate);
		return JsonResult.success(recentLoginUsers);
	}

	/**
	 * 获取总用户数
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "/internal/user/allusers/count", method = RequestMethod.GET)
	public JsonResult<Integer> getAllUsers(@RequestParam("endDate") Long endDate) {
		Integer allUsers = sysUserService.getAllUsers(endDate);
		return JsonResult.success(allUsers);
	}
	
	/**
	 * 获取总用户数
	 * @return
	 */
	@RequestMapping(value = "/internal/user/online/count", method = RequestMethod.GET)
	public JsonResult<Integer> getOnlineCount() {
		return JsonResult.success(tokenCacheService.getOnlineCount());
	}

}
